package com.datasure.cloudsure.redis.provider.impl;

import com.datasure.cloudsure.redis.builder.RedisConnectionFactoryBuilder;
import com.datasure.cloudsure.redis.provider.RedisProvider;
import com.datasure.cloudsure.redis.config.property.DynamicRedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 多数据源连接工厂提供者
 */
public class YamlRedisProvider implements RedisProvider {

    public DynamicRedisProperties dynamicRedisProperties;

    public YamlRedisProvider(DynamicRedisProperties properties) {
        this.dynamicRedisProperties = properties;
    }

    public DynamicRedisProperties getDynamicRedisProperties() {
        return dynamicRedisProperties;
    }

    public void setDynamicRedisProperties(DynamicRedisProperties dynamicRedisProperties) {
        this.dynamicRedisProperties = dynamicRedisProperties;
    }

    @Override
    public Map<String, RedisConnectionFactory> loadRedis() {
        // 获取多个Redis配置参数
        Map<String, RedisProperties> connection = this.dynamicRedisProperties.getConnection();
        Map<String, RedisConnectionFactory> connectionMap = new HashMap<>(connection.size());
        connection.forEach((key, value)-> {
            connectionMap.computeIfAbsent(key, k -> RedisConnectionFactoryBuilder.build(value));
        });

        return connectionMap;
    }
}
